﻿using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using HappyTravelling.Resources;
using HappyTravelling.ThanhService;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using Telerik.Windows.Controls;

namespace HappyTravelling.Views
{
    public partial class MainPageContent : UserControl
    {
        
        public event EventHandler EditEvent , ViewEvent;
        public MainPageContent()
        {
            InitializeComponent();
        }

        private void ListDbInCard_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            WebService1SoapClient client = new WebService1SoapClient();
            MeetingDetail.dlm = ListMeetingBox.SelectedItem as DisplayListMeeting;
            if (!MainPage.isOpenListInvite)
            {
                ViewEvent(sender, e);
            }
            else
            {
                client.ResponeToInvitAsync(App.CurrentUser.id, (ListMeetingBox.SelectedItem as DisplayListMeeting).ID, 1);
            }
            
        }

        public void GetData()
        {
            try
            {
                WebService1SoapClient client = new WebService1SoapClient();
                client.GetAllMyMeetingsAsync(App.CurrentUser.id);
                client.GetAllMyMeetingsCompleted += GetAllMyMeetingsCompleted;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

        private void GetAllMyMeetingsCompleted(object sender, GetAllMyMeetingsCompletedEventArgs e)
        {
            string json = e.Result;
            var ListMeeting = JsonConvert.DeserializeObject<List<Meeting>>(json);
            List<DisplayListMeeting> ldMeetings = new List<DisplayListMeeting>();
            foreach (var meeting in ListMeeting)
            {
                if (meeting != null)
                {
                    ldMeetings.Add(new DisplayListMeeting
                    {
                        ID = meeting.Id,
                        Location = new GeoCoordinate(meeting.Latitude, meeting.Longtitude),
                        TenAdmin = meeting.Leader.DisplayName,
                        TenCuocHen = meeting.Content,
                        Time = meeting.Time.ToShortDateString()
                    });
                }
            }
            try
            {
                ListMeetingBox.ItemsSource = ldMeetings;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void RcmContext_OnTapped(object sender, Telerik.Windows.Controls.ContextMenuItemSelectedEventArgs e)
        {
            try
            {
                var menuItem = (RadContextMenuItem)sender;
                string id = menuItem.Tag.ToString();
                if (menuItem.Content.ToString() == AppResources.edit)
                {

                    EditEvent(sender, e);
                    //client.GetMeetingByIdAsync(Convert.ToInt32(id));
                    //client.GetMeetingByIdCompleted += 
                    //(a1, e1) =>
                    //{
                    //    string json = e1.Result;
                    //    var Meeting = JsonConvert.DeserializeObject<Meeting>(json);
                    //    client.EditMeetingAsync();
                    //};
                }
                else if (menuItem.Content.ToString() == AppResources.wpViewMode)
                {
                    ViewEvent(sender, e);
                }
                else if (menuItem.Content.ToString() == AppResources.delete)
                {
                }
            }
            catch
            {
            }
        }

    }
}
